#include <git2.h>
#include <stdio.h>
#include <string.h>

int main() {
	git_libgit2_init();
	char *repo_path = "test_repos";
	git_repository *repo;
	int i;
	char out[GIT_OID_HEXSZ+1];

	int error = git_repository_open(&repo ,repo_path);
	if(error<0) {
		printf("git_repository_open failed, error msg:%s\n", giterr_last()->message);
		return -1;
	}

	//head
	git_reference *head;
	git_repository_head(&head, repo);

	const git_oid *oid = git_reference_target(head);
	if(oid == NULL) {
		printf("git_reference_target failed, code message:%s\n", giterr_last()->message);
		git_repository_free(repo);
		return -1;
	}

	// create ref
	git_reference *ref;
	const char* ref_name = "refs/heads/zql/new_r";
	if(git_reference_create(&ref, repo, ref_name, oid, 1, "create ref") < 0) {
		printf("git_reference_create failed, error msg:%s\n", giterr_last()->message);
		goto cleanup;
	}
	git_reference_free(ref);

	git_commit *commit;
	if (git_commit_lookup(&commit, repo, oid) < 0) {
		printf("git_commit_lookup failed, error msg:%s\n", giterr_last()->message);
		goto cleanup;
	}

	// create branch
	git_reference *branch;
	const char *branch_name = "zql/new_b";
	if(git_branch_create(&branch, repo, branch_name, commit, 1) < 0) {
		printf("git_branch_create failed, error msg:%s\n", giterr_last()->message);
		goto cleanup;
	}
	git_reference_free(branch);

	// create tag
	git_oid tag_oid;
	if(git_tag_create(&tag_oid, repo, "tag_v1.0", (const git_object *)commit, git_commit_author(commit), "create tag", 1) < 0) {
		printf("git_tag_create failed, error msg:%s\n", giterr_last()->message);
		goto cleanup;
	}

	git_commit_free(commit);

	cleanup:
		git_reference_free(head);
		git_repository_free(repo);
	return 0;
}
